package server;

import server.Exceptions.AuthenticationConflictException;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class MessageProcessor {
    public static final String SEND = "/snd";
    public static final String HISTORY = "/hist";
    public static final String SET_NAME = "/chid";
    public static final String CHANGE_ROOM = "/chroom";
    public static final String PRIVATE_MSG = "/sdnp";
    public static final String REGISTRATION = "/reg";
    public static final String PASSWORD = "/pwd";
    Server server;

    public MessageProcessor(Server server) {
        this.server = server;
    }

    public void process(final Client client, final String inputMessage) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = new Date();
        String outputMessage = "";
        String[] args = inputMessage.split(" ");

        if (client.isNewClient()) {
            if (newClient(client, args)) return;

        }
        switch (args[0]) {
            case REGISTRATION:
                registration(client, args);
                break;

            case SET_NAME:
                setName(client, dateFormat.format(date),args);
                break;

            case SEND:
                send(client, inputMessage, dateFormat.format(date));
                break;

            case HISTORY:
                history(client, args);
                break;

            case CHANGE_ROOM:
                changeRoom(client, dateFormat.format(date), args);
                break;

            case PRIVATE_MSG:
                privateMessage(client, inputMessage, dateFormat.format(date), args);
                break;
            
            default:
                server.sendMessage(client, "Please, use command '/snd', if you want to send a message");
                break;


        }
    }

    public void history(Client client, String[] args) {
        if (args.length == 1) {
            List<Message> messages = DataBase.getHistoryByName(client.getName());
            StringBuilder builder = new StringBuilder();
            for (Message message : messages) {
                builder.append(message.toString()).append("\n");
            }
            server.sendMessage(client, builder.toString());
        }
        if (args.length == 2) {
            List<Message> messages = DataBase.getHistoryByRoom(client.getRoom());
            StringBuilder builder = new StringBuilder();
            for (Message message : messages) {
                builder.append(message.toString()).append("\n");
            }
            server.sendMessage(client, builder.toString());
        }
    }

    public void privateMessage(Client client, String inputMessage, String dateFormat, String[] args) {
        String outputMessage;
        String textPrivate = inputMessage.substring(PRIVATE_MSG.length());
        if (textPrivate.length() <= 150) {
            outputMessage = client + ": " + inputMessage.substring(PRIVATE_MSG.length());
            server.sendMessageToAll(new Message(client, args[1], client.getRoom(), dateFormat, outputMessage));
        } else {
            outputMessage = "The message contains more than 150 characters";
            server.sendMessage(client, outputMessage);
        }
    }

    public void changeRoom(Client client, String format, String[] args) {
        String outputMessage;
        client.setRoom(args[1]);
        outputMessage = "New user " + client.getName() + " in room " + client.getRoom();
        server.sendMessageToAll(new Message(client, null, client.getRoom(), format, outputMessage));
    }

    public void send(Client client, String inputMessage, String dateFormat) {
        String outputMessage;
        String text = inputMessage.substring(SEND.length());
        if (text.length() <= 150) {
            outputMessage = text;
            server.sendMessageToAll(new Message(client, null, client.getRoom(), dateFormat, outputMessage));
        } else {
            outputMessage = "The message contains more than 150 characters";
            server.sendMessage(client, outputMessage);
        }
    }

    public void setName(Client client, String dateFormat, String[] args) {
        String outputMessage;
        if (args.length != 3) {
            server.sendMessage(client, "Wrong input data. Try again");
            return;
        }
        if (!server.ifNameRegistered(args[1])) {
            server.sendMessage(client, "Wrong nick. Try again");
        } else {
            if (!server.ifCorrectPassword(args[2])){
                server.sendMessage(client, "Wrong password");
            }
            try {
                if (server.addClientToChat(args[1], args[2], client)) {
                    outputMessage = "New user " + args[1] + " in GuestRoom";
                    server.sendMessageToAll(new Message(client, null, client.getRoom(), dateFormat, outputMessage));
                }
            } catch (AuthenticationConflictException e) {
                server.sendMessage(client, "Client with this nick is already online");
            }
        }
    }

    public void registration(Client client, String[] args) {
        if (args.length != 3) {
            server.sendMessage(client, "Wrong input data. Try again");
        } else {
            server.addNewClientToSystem(args[1], args[2]);
            server.sendMessage(client, "Registration was successful. Now you may sign in ");
        }
        return;
    }

    public boolean newClient(Client client, String[] args) {
        if (args.length == 0) {
            server.sendMessage(client, "Please, use command '/chid' or command '/reg'");
        } else {
            if (!args[0].equals(SET_NAME) && !args[0].equals(REGISTRATION)) {
                server.sendMessage(client, "Please, use command '/chid' or command '/reg'");
                return true;
            }
        }
        return false;
    }
}

